﻿using System;
using System.Linq;
using System.Security.Claims;
using Abp.Dependency;
using Abp.Runtime.Session;

namespace Hicap.AuthorizationServer.Core.Common
{
    public static class SessionExtension
    {
        public static int? GetCompanyId(this IAbpSession abpSession)
        {
            var principal = IocManager.Instance.Resolve<IPrincipalAccessor>().Principal;

            Claim claim = principal != null
                ? principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.GroupSid)
                : (Claim)null;

            if (!string.IsNullOrEmpty(claim?.Value))
                return new int?(Convert.ToInt32(claim.Value));

            return new int?();
        }

        public static string GetAuthCodes(this IAbpSession abpSession)
        {
            var principal = IocManager.Instance.Resolve<IPrincipalAccessor>().Principal;

            Claim claim = principal != null
                ? principal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.AuthorizationDecision)
                : (Claim)null;

            return claim?.Value;
        }
    }
}